#include "stdafx.h"
#include "ckMetaGroup.h"
#include "ckValue.h"

namespace ck
{
	namespace data
	{
		MetaGroup::MetaGroup()
		{
			m_iter = m_pairs.begin();
		}
		
		MetaGroup::~MetaGroup()
		{
			Cleanup();
		}

		void MetaGroup::Cleanup()
		{
			Value* pValue = NULL;
			std::map<std::string, Value*>::iterator iter;
			for (iter = m_pairs.begin(); iter != m_pairs.end(); iter++)
			{
				pValue = iter->second;
				if (pValue)
				{
					pValue->Release();
				}
			}
			m_pairs.clear();
		}

		RESULTCODE MetaGroup::Set(const char* szKey, Value* pValue)
		{
			if (szKey == NULL)// || pValue == NULL)
			{
				return RC_FAILURE;
			}

			std::string strKey = szKey;
			Value* ptr = m_pairs[strKey];
			if (ptr)
			{
				ptr->Release();
			}
			m_pairs[strKey] = pValue;

			return RC_SUCCESS;
		}

		void MetaGroup::Reset()
		{
			m_iter = m_pairs.begin();
		}

		void MetaGroup::Next()
		{
			if (m_iter != m_pairs.end())
			{
				m_iter++;
			}
		}

		bool MetaGroup::IsEOF()
		{
			return (m_iter == m_pairs.end());
		}
		
		const char* MetaGroup::GetKey()
		{
			if (m_iter == m_pairs.end())
			{
				return NULL;
			}
			return m_iter->first.c_str();
		}

		Value* MetaGroup::GetValue()
		{
			if (m_iter == m_pairs.end())
			{
				return NULL;
			}
			return m_iter->second;
		}

		RESULTCODE MetaGroup::SetName(const char* szName)
		{
			if (szName == NULL)
			{
				return RC_FAILURE;
			}

			m_name = szName;

			return RC_SUCCESS;
		}
		const char*	MetaGroup::GetName()
		{
			return m_name.c_str();
		}

	}
}
